<?php /* narato/vivasky */

function narato_setup_admin_js(){
  wp_enqueue_script('narato-settings', THEME_URL.'/admin/theme-settings.js', array('jquery'));
  wp_enqueue_script('narato-settings-module', THEME_URL.'/admin/theme-settings.modules.dev.js');
  wp_enqueue_script('theme-install');
  wp_enqueue_script('theme-preview');
  ?>
 <?php
}

function get_upload_dir($dir) {
  $uploadpath = wp_upload_dir();
  if ($uploadpath['baseurl']=='') $uploadpath['baseurl'] = get_bloginfo('siteurl').'/wp-content/uploads';
  return $uploadpath[$dir];
}

// load theme preview iframe with ajax (faster access to theme settings)
function narato_get_site_preview() {
  check_ajax_referer("site_preview"); ?>
  <iframe id="themepreview" name="themepreview" src="<?php echo get_option('home'); ?>/?preview=1"></iframe>
  <?php die();
}

function narato_admin_init_js(){
  $nonce = wp_create_nonce('site_preview');
  $font_styles = font_styles();
  $cnt_font= count($font_styles);
  $current_settings = get_option('narato');
  ?>
  <script type="text/javascript">
  /* <![CDATA[ */
	
	NaratoCfg.Init({
		fonts:[
			<?php foreach ($font_styles as $i => $value):?>
			{ 'i': '<?php echo $i; ?>','v':'<?php echo $font_styles[$i]['code']; ?>'}<?php if($i<$cnt_font-1):?>,<?php echo PHP_EOL;?><?php else:?><?php echo PHP_EOL;?><?php endif;?>
			<?php endforeach; ?>
		],
		layout:'<?php echo($current_settings['page_width'] == 'fluid' ? 'fluid' : 'fixed'); ?>',
		layout_type:'<?php echo $current_settings['layout']; ?>',
		url_theme:'<?php echo THEME_URL; ?>',
		bg:'<?php echo get_narato_option("background");?>',
		ajax_nonce:'<?php echo $nonce; ?>'	
	});
	
  <?php do_action("narato_admin_init_js"); ?>

  /* ]]> */
  </script>
 <?php
}

function narato_setup_admin_css($dir) {
  wp_register_style('narato-settings', THEME_URL. '/admin/theme-settings.css');
  wp_enqueue_style('narato-settings');
  wp_enqueue_style('theme-install');
}

function is_valid_image($image){
  // check mime type
  if(!eregi('image/', $_FILES[$image]['type'])):
   wp_redirect(admin_url('themes.php?page=theme-settings&error=1'));
   exit(0);
  endif;

  // check if valid image
  $imageinfo = getimagesize($_FILES[$image]['tmp_name']);
  if($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg' && $imageinfo['mime'] != 'image/png' && isset($imageinfo)):
   wp_redirect(admin_url('themes.php?page=theme-settings&error=2'));
   exit(0);
  endif;

  list($width, $height) = $imageinfo;

  $directory = get_upload_dir('basedir').'/';
  if(!@move_uploaded_file($_FILES[$image]['tmp_name'],$directory.$_FILES[$image]["name"])):
   wp_redirect(admin_url('themes.php?page=theme-settings&error=3'));
   exit(0);
  else:
   return $width.'x'.$height;
  endif;
}

function narato_update_options() {
  check_admin_referer('theme-settings');

  // enable theme settings for lower level users, but with limitations
  if(!current_user_can('switch_themes')) wp_die(__('You are not authorised to perform this operation.', 'narato'));
  $options = get_option('narato');

  foreach (narato_default_settings() as $key => $value):
   $options[$key] = stripslashes((string)$_POST[$key]);
   if(($key == 'exclude_pages') && ($_POST[$key] != '')) $options[$key] = implode(',', $_POST[$key]);// else $options['exclude_pages'] = '';
   if(($key == 'exclude_categories') && ($_POST[$key] != '')) $options[$key] = implode(',', $_POST[$key]);// else $options['exclude_categories'] = '';

   // filter potentially malicious html/css (eg <script>, onclick, css expressions etc)
   if(!current_user_can('unfiltered_html')) $options[$key] = narato_strip_tags_attributes($options[$key]);
  endforeach;

  // build dimensions array
  $dimensions = get_narato_option('dimensions');
  foreach($dimensions as $layout_size => $layout_types)
   foreach($layout_types as $layout => $values)
    $options['dimensions'][$layout_size][$layout] = $_POST['dimensions_'.$layout_size.'_'.$layout];

  if(isset($_POST['remove-logo'])):
   $options['logo'] = '';
   $options['logo_size'] = '';
  elseif($_FILES["file-logo"]["type"]):
   $valid = is_valid_image('file-logo');
   if($valid):
    $options['logo'] = get_upload_dir('baseurl'). "/". $_FILES["file-logo"]["name"];
    $options['logo_size'] = $valid;
   endif;
  endif;

  if(isset($_POST['remove-background'])):
   $options['background'] = '';
  elseif($_FILES["file-background"]["type"]):
   $valid = is_valid_image('file-background');
   if($valid):
    $options['background'] = get_upload_dir('baseurl'). "/". $_FILES["file-background"]["name"];
   endif;
  endif;

  update_option('narato', $options);

  // reset?
  if (isset($_POST['reset'])) narato_setup_options();

  wp_redirect(admin_url('themes.php?page=theme-settings&updated=true'));
}

// from themes.php
function narato_check_update() {
  static $themes_update;
  $theme = get_theme("narato");
  if (!isset($themes_update)) $themes_update = get_transient('update_themes');

  if (is_object($theme) && isset($theme->stylesheet)) $stylesheet = $theme->stylesheet;
  elseif (is_array($theme) && isset($theme['Stylesheet'])) $stylesheet = $theme['Stylesheet'];
  else return;

  if (isset($themes_update->response[$stylesheet])):
    $update = $themes_update->response[$stylesheet];
    $theme_name = is_object($theme) ? $theme->name : (is_array($theme) ? $theme['Name'] : '');
    $details_url = add_query_arg(array('TB_iframe' => 'true', 'width' => 1024, 'height' => 800), $update['url']);
    $update_url = wp_nonce_url('update.php?action=upgrade-theme&amp;theme='.urlencode($stylesheet), 'upgrade-theme_'.$stylesheet);
    echo '<div class="updated fade below-h2"><p>';
    if (!current_user_can('update_themes') || empty($update->package))
      printf(__('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%1$s">View version %3$s Details</a>.'), $theme_name, $details_url, $update['new_version']);
    else printf(__('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%1$s">View version %3$s Details</a> or <a href="%4$s" >upgrade automatically</a>.'), $theme_name, $details_url, $update['new_version'], $update_url);
    echo '</div>';
  endif;
}

function narato_selected_class($value, $condition, $output = 'radio-selected'){
  if($condition == $value) return $output;
}


function narato_theme_settings() {
  global $is_IE;
  if(!current_user_can('switch_themes')) wp_die(__('You are not authorised to perform this operation.', 'narato')); ?>

  <div id="narato-settings" class="wrap clearfix">
   <form action="<?php echo admin_url('admin-post.php?action=narato_update'); ?>" method="post" enctype="multipart/form-data">

   <div class="clearfix">
     <?php screen_icon(); ?><h2 class="alignleft"><?php _e('narato settings','narato'); ?></h2>
     <p class="controls alignleft"><input type="submit" class="button-primary" name="submit" value="<?php _e("Save Changes","narato"); ?>" /><input type="submit" class="button-primary" name="reset" value="<?php _e("Reset to Defaults","narato"); ?>" onclick="if(confirm('<?php _e("Reset all theme settings to the default values? Are you sure?", "narato"); ?>')) return true; else return false;" /></p>
   </div>

   <?php wp_nonce_field('theme-settings'); ?>

   <?php narato_check_update(); ?>

   <?php if (isset($_GET['updated'])): ?>
   <div class="updated fade below-h2">
    <p><?php printf(__('Settings saved. %s', 'narato'),'<a href="' . user_trailingslashit(get_bloginfo('url')) . '">' . __('View site','narato') . '</a>'); ?></p>
   </div>
   <?php elseif (isset($_GET['error'])):
     $errors  = array(
       1 => __("Please upload a valid image file!","narato"),
       2 => __("The file you uploaded doesn't seem to be a valid JPEG, PNG or GIF image","narato"),
       3 => __("The image could not be saved on your server","narato")
     );

   ?>
   <div class="error fade below-h2">
    <p><?php printf(__('Error: %s', 'narato'),$errors[$_GET['error']]); ?></p>
   </div>
   <?php endif; ?>

   <!-- tabbed content -->
   <div id="narato-settings-tabs">

    <div class="clearfix">
     <ul class="subsubsub">
      <li class="design"><a href='#tab-design'><?php _e("Design","narato"); ?></a></li> |
      <li class="content"><a href='#tab-content'><?php _e("Content","narato"); ?></a></li> |
      <li class="nav"><a href='#tab-navigation'><?php _e("Navigation","narato"); ?></a></li> |
      <li class="seo"><a href='#tab-seo'><?php _e("SEO","narato"); ?></a></li> |
      <li class="ads"><a href='#tab-ads'><?php _e("Ads","narato"); ?></a></li> |
      <li class="adv"><a href='#tab-advanced'><?php _e("Advanced","narato"); ?></a></li> |
      <li class="usercss"><a href='#tab-css'><?php _e("User CSS","narato"); ?></a></li> |
     </ul>
    </div>

    <!-- sections -->
    <div class="sections wrap metabox-holder">

     <div class="section" id="tab-design">

      <?php if($is_IE): ?>
      <p><?php _e("The design settings panel is not yet supported by Internet Explorer. Sorry :(","narato"); ?></p>
      <?php else: ?>

      <div id="themepreview-wrap"><div class="clearfix"><div class="loading"><?php _e("Loading site preview...","narato"); ?></div></div></div>

      <table class="form-table">
       <?php $layout = get_narato_option('layout'); ?>
       <tr>
        <th scope="row"><p><?php _e("Layout style","narato") ?><span><?php _e("Use page templates if you want to apply these only to specific pages","narato"); ?></span></p></th>
        <td id="layout-settings" class="clearfix">
         <div class="layout-box">
          <label for="layout-settings-g-col1" class="layout g-col1 <?php echo narato_selected_class('g-col1', $layout); ?>"></label>
          <input class="radio" type="radio" name="layout" id="layout-settings-g-col1" value="g-col1" <?php checked('col-1', $layout); ?>/>
         </div>

         <div class="layout-box">
          <label for="layout-settings-g-col2-mr" class="layout g-col2-mr <?php echo narato_selected_class('g-col2-mr', $layout); ?>"></label>
          <input class="radio" type="radio" name="layout" id="layout-settings-g-col2-mr" value="g-col2-mr" <?php checked('g-col2-mr', $layout); ?>/>
         </div>

         <div class="layout-box">
          <label for="layout-settings-g-col2-ml" class="layout g-col2-ml <?php echo narato_selected_class('g-col2-ml', $layout); ?>"></label>
          <input class="radio" type="radio" name="layout" id="layout-settings-g-col2-ml" value="g-col2-ml" <?php checked('g-col2-ml', $layout); ?>/>
         </div>

         <div class="layout-box">
          <label for="layout-settings-g-col3-mm" class="layout g-col3-mm <?php echo narato_selected_class('g-col3-mm', $layout); ?>"></label>
          <input class="radio" type="radio" name="layout" id="layout-settings-g-col3-mm" value="g-col3-mm" <?php checked('g-col3-mm', $layout); ?>/>
         </div>

         <div class="layout-box">
          <label for="layout-settings-g-col3-mr" class="layout g-col3-mr <?php echo narato_selected_class('g-col3-mr', $layout); ?>"></label>
          <input class="radio" type="radio" name="layout" id="layout-settings-g-col3-mr" value="g-col3-mr" <?php checked('g-col3-mr', $layout); ?>/>
         </div>

         <div class="layout-box">
          <label for="layout-settings-g-col3-ml" class="layout g-col3-ml <?php echo narato_selected_class('g-col3-ml', $layout); ?>"></label>
          <input class="radio" type="radio" name="layout" id="layout-settings-g-col3-ml" value="g-col3-ml" <?php checked('g-col3-ml', $layout); ?>/>
         </div>

        </td>
       </tr>

       <tr>
        <th scope="row"><p><?php _e("Color scheme","narato"); ?></p></th>
        <td id="color-scheme">

         <div class="color-box">
          <label for="color-box-green" class="color_scheme green"></label>
          <input class="radio" type="radio" name="color_scheme" id="color-box-green" value="green" <?php checked('green', get_narato_option('color_scheme')) ?>/>
         </div>

         <div class="color-box">
          <label for="color-box-blue" class="color_scheme blue"></label>
          <input class="radio" type="radio" name="color_scheme" id="color-box-blue" value="blue" <?php checked('blue', get_narato_option('color_scheme')) ?>/>
         </div>

         <div class="color-box">
          <label for="color-box-red" class="color_scheme red"></label>
          <input class="radio" type="radio" name="color_scheme" id="color-box-red" value="red" <?php checked('red', get_narato_option('color_scheme')) ?>/>
         </div>

         <div class="color-box">
          <label for="color-box-grey" class="color_scheme grey"></label>
          <input class="radio" type="radio" name="color_scheme" id="color-box-grey" value="grey" <?php checked('grey', get_narato_option('color_scheme')) ?>/>
         </div>

        </td>
       </tr>

       <tr>
        <th scope="row"><p><?php _e("Page (content) width","narato"); ?><span><?php _e("Note that fluid pages can be hard to read on large resolutions.","narato"); ?></span></p></th>
        <td>

         <label for="opt_page_width_fixed"><input name="page_width" type="radio" id="opt_page_width_fixed" class="radio" value="fixed" <?php checked('fixed', get_narato_option('page_width')) ?> /><?php printf(__("Fixed (%s)","narato"),'<a href="http://960.gs/" target="_blank">960gs</a>'); ?></label>
         <label for="opt_page_width_fluid"><input name="page_width" type="radio" id="opt_page_width_fluid" class="radio" value="fluid" <?php checked('fluid', get_narato_option('page_width')) ?> /><?php _e("Fluid (100%)/Custom","narato"); ?></label>

        </td>
       </tr>

       <tr id="dimension_controls"<?php if(get_narato_option('layout') == 'g-col1'): ?> class="hidden"<?php endif; ?>>
        <th scope="row"><p><?php _e("Column sizes","narato"); ?><span><?php _e("(With 10 pixel padding between them)","narato"); ?></span></p></th>
        <td id="slider-wrap">
         <div id="slider"></div>
         <?php
           $dimensions = get_narato_option('dimensions');
           foreach($dimensions as $layout_size => $layout_types)
            foreach($layout_types as $layout => $values)
              echo '<input type="hidden" id="opt_dimensions_'.$layout_size.'_'.$layout.'" name="dimensions_'.$layout_size.'_'.$layout.'" value="'.$values.'" />'.PHP_EOL;
         ?>
        </td>
       </tr>

       <tr>
        <th scope="row"><p><?php _e("Font style","narato"); ?></p></th>
        <td>
          <?php $font_styles = font_styles(); ?>

          <select name="font_style" id="opt_font_style">
           <?php foreach ($font_styles as $entry => $name): ?>
           <option value="<?php echo $entry; ?>" <?php selected($entry, get_narato_option('font_style')); ?> style='font-size:100%; font-family: <?php echo $font_styles[$entry]['code']; ?>;'><?php echo $font_styles[$entry]['desc']; ?></option>
           <?php endforeach; ?>
         </select>
        </td>
       </tr>

       <tr>
        <th scope="row"><p><?php _e("Background color","narato"); ?><span><?php _e("Leave Black (#000000) to keep the default background","narato"); ?></span></p></th>
        <td>

           <div class="color-selector clearfix" id="opt_background_color">
             <div class="preview" style="background-color: #<?php echo wp_specialchars(get_narato_option('background_color')); ?>">
               <input name="background_color" type="hidden" value="<?php echo wp_specialchars(get_narato_option('background_color')); ?>" />
             </div>
           </div>

        </td>
       </tr>

       <tr>
        <th scope="row"><p><?php _e("Custom logo image","narato"); ?><span><?php _e("Show a logo image instead of text; Upload the graphic file from your computer","narato"); ?></span></p></th>
        <td>
          <?php if(is_writable(get_upload_dir('basedir'))): ?>
           <input type="file" name="file-logo" id="file-logo" />
           <?php if(get_narato_option('logo')): ?>
           <button type="submit" class="button" name="remove-logo" value="0"><?php _e("Remove current image","narato"); ?></button>
           <div class="clearfix">
            <div class="image-preview"><img src="<?php echo get_narato_option('logo'); ?>" style="padding:10px;" /></div>
           </div>
           <?php endif; ?>
         <?php else: ?>
         <p class="error" style="padding: 4px;"><?php printf(__("Directory %s doesn't have write permissions - can't upload!","narato"),'<strong>'.get_upload_dir('basedir').'</strong>'); ?></p><p><?php _e("Check your upload path in Settings/Misc or CHMOD this directory to 755/777.<br />Contact your host if you don't know how","narato"); ?></p>
         <?php endif; ?>
         <input type="hidden" name="logo" value="<?php echo get_narato_option('logo'); ?>">
         <input type="hidden" name="logo_size" value="<?php echo get_narato_option('logo_size'); ?>">
        </td>
       </tr>


       <tr>
        <th scope="row"><p><?php _e("Custom background image","narato"); ?><span><?php _e("Upload a new background/header image to replace the default one","narato"); ?></span></p></th>
        <td>
         <?php if(is_writable(get_upload_dir('basedir'))): ?>
           <input type="file" name="file-background" id="file-background" />
           <?php if(get_narato_option('background')): ?>
           <button type="submit" class="button" name="remove-background" value="0"><?php _e("Remove current image","narato"); ?></button>
           <div class="clearfix">
            <div class="image-preview"><img src="<?php echo get_narato_option('background'); ?>" style="padding:10px;" /></div>
           </div>
           <?php endif; ?>
         <?php else: ?>
         <p class="error" style="padding: 4px;"><?php printf(__("Directory %s doesn't have write permissions - can't upload!","narato"),'<strong>'.get_upload_dir('basedir').'</strong>'); ?></p><p><?php _e("Check your upload path in Settings/Misc or CHMOD this directory to 755/777.<br />Contact your host if you don't know how","narato"); ?></p>
         <?php endif; ?>
         <input type="hidden" name="background" value="<?php echo get_narato_option('background'); ?>">
        </td>
       </tr>

      </table>
     <?php endif; ?>
     </div>

     <div class="section" id="tab-content">
      <table class="form-table">

       <tr>
        <th scope="row"><p><?php _e("Posts (single)","narato"); ?><span><?php _e("How much information do you want to show on the single post page?", "narato"); ?></span></p></th>
        <td>
         <label for="opt_single_tags">
           <input name="post_single_tags" id="opt_single_tags" type="checkbox" class="checkbox" value="1" <?php checked('1', get_narato_option('post_single_tags')) ?> /> <?php _e("Tags", "narato"); ?>
         </label>
         <label for="opt_single_meta">
           <input name="post_single_meta" id="opt_single_meta" type="checkbox" class="checkbox" value="1" <?php checked('1', get_narato_option('post_single_meta')) ?> /> <?php _e("Meta", "narato"); ?>
         </label>
         <label for="opt_single_share">
          <input name="post_single_share" id="opt_single_share" type="checkbox" class="checkbox" value="1" <?php checked('1', get_narato_option('post_single_share')) ?> /> <?php _e("Share", "narato"); ?>
         </label>
         <label for="opt_single_author">
           <input name="post_single_author" id="opt_single_author" type="checkbox" class="checkbox" value="1" <?php checked('1', get_narato_option('post_single_author')) ?> /> <?php _e("About the author", "narato"); ?>
         </label>
         <label for="opt_single_print">
          <input name="post_single_print" id="opt_single_print" type="checkbox" class="checkbox" value="1" <?php checked('1', get_narato_option('post_single_print')) ?> /> <?php _e("Print link", "narato"); ?>
         </label>
         <label for="opt_single_related">
           <input name="post_single_related" id="opt_single_related" type="checkbox" class="checkbox" value="1" <?php checked('1', get_narato_option('post_single_related')) ?> /> <?php _e("Related posts", "narato"); ?>
         </label>
        </td>
       </tr>

       <tr>
        <th scope="row"><p><?php _e("Post previews","narato"); ?><span><?php _e("How much info do you want to show on blog/category/archive/search pages?", "narato"); ?></span></p></th>
        <td>
         <label for="opt_post_title">
           <input name="post_title" id="opt_post_title" type="checkbox" class="checkbox" value="1" <?php checked('1', get_narato_option('post_title')) ?> /> <?php _e("Title", "narato"); ?>
         </label>
         <label for="opt_post_info">
           <input name="post_info" id="opt_post_info" type="checkbox" class="checkbox" value="1" <?php checked('1', get_narato_option('post_info')) ?> /> <?php _e("Info bar", "narato"); ?>
         </label>
         <label for="opt_post_tags">
          <input name="post_tags" id="opt_post_tags" type="checkbox" class="checkbox" value="1" <?php checked('1', get_narato_option('post_tags')) ?> /> <?php _e("Tags"); ?>
         </label>
         <label for="opt_post_content" style="margin-right: 0;">
          <input name="post_content" id="opt_post_content" type="checkbox" class="checkbox" value="1" <?php checked('1', get_narato_option('post_content')) ?> /> <?php _e("Content", "narato"); ?>
         </label>
         <select name="post_content_length" id="opt_post_content_length"<?php if(!get_narato_option('post_content')): ?> disabled="disabled"<?php endif; ?>>
           <option value="50" <?php selected('50', get_narato_option('post_content_length')); ?>><?php _e('max. 50 words, filtered', 'narato'); ?></option>
           <option value="100" <?php selected('100', get_narato_option('post_content_length')); ?>><?php _e('max. 100 words, filtered', 'narato'); ?></option>
           <option value="200" <?php selected('200', get_narato_option('post_content_length')); ?>><?php _e('max. 200 words, filtered', 'narato'); ?></option>
           <option value="f" <?php selected('f', get_narato_option('post_content_length')); ?>><?php _e('full', 'narato'); ?></option>
           <option value="ff" <?php selected('ff', get_narato_option('post_content_length')); ?>><?php _e('full, filtered', 'narato'); ?></option>
           <option value="e" <?php selected('e', get_narato_option('post_content_length')); ?>><?php _e('excerpt', 'narato'); ?></option>
         </select>
        </td>
       </tr>

       <tr>
         <th scope="row"><p><?php _e("Auto generate thumbnails","narato"); ?><span><?php _e("Get the 1st image attached to a post, if no thumbnail is set manually","narato"); ?></span></p></th>
         <td><input name="post_thumb_auto" type="checkbox" class="checkbox" value="1" <?php checked( '1', get_narato_option('post_thumb_auto')) ?> /></td>
       </tr>

       <tr>
        <th scope="row"><p><?php _e("Post thumbnail size","narato"); ?><span><?php printf(__("Note that this only works for images you upload from now on, older images will be browser-resized. You should use the %s plugin to create missing image sizes","narato"),'<a href="http://wordpress.org/extend/plugins/regenerate-thumbnails/" target="_blank">Regenerate Thumbnails</a>'); ?></span></p></th>
        <td>
         <select name="post_thumb">
          <?php $wpsize = get_option('thumbnail_size_w').' x '.get_option('thumbnail_size_h'); ?>
          <option value="48x48" <?php selected('48x48', get_narato_option('post_thumb')); ?>><?php _e('Very small: 48 x 48', 'narato'); ?></option>
          <option value="64x64" <?php selected('64x64', get_narato_option('post_thumb')); ?>><?php _e('Small: 64 x 64', 'narato'); ?></option>
          <option value="80x80" <?php selected('80x80', get_narato_option('post_thumb')); ?>><?php _e('Medium: 80 x 80', 'narato'); ?></option>
          <option value="100x100" <?php selected('100x100', get_narato_option('post_thumb')); ?>><?php _e('Larger: 100 x 100', 'narato'); ?></option>
          <option value="<?php echo str_replace (" ","",$wpsize); ?>" <?php selected(str_replace (" ","",$wpsize), get_narato_option('post_thumb')); ?>><?php printf(__("WP's Media setting: %s", "narato"),$wpsize); ?></option>
         </select>
        </td>
       </tr>

       <?php do_action("narato_admin_content"); ?>

       <tr>
        <th scope="row"><p><?php _e("Footer content","narato"); ?><span><?php  if(current_user_can('unfiltered_html')) _e("You can post HTML code",'narato'); else _e("Only some HTML tags and attributes are allowed",'narato'); ?></span></p></th>
        <td>
         <textarea id="opt_footer_content" rows="8" cols="60" name="footer_content" class="code"><?php echo wp_specialchars(get_narato_option('footer_content')); ?></textarea>
         <br />
         <?php printf(__("Use the following short codes for convenient adjustments: <br />%s",'narato'),'<code>[rss]</code> <code>[copyright]</code> <code>[credit]</code> <code>[ad code=#]</code> <code>[wp-link]</code> <code>[theme-link]</code> <code>[login-link]</code> <code>[blog-title]</code> <code>[xhtml]</code> <code>[css]</code> <code>[top]</code> <code>[page-rank]</code>.'); ?>
        </td>
       </tr>

      </table>
     </div>

     <div class="section" id="tab-navigation">
      <table class="form-table">
       <tr>
        <th scope="row"><p><?php _e("Top navigation shows","narato"); ?></p></th>
        <td class="clearfix">
         <select name="navigation" id="opt_navigation" class="alignleft">
          <option value="0" <?php selected('0', get_narato_option('navigation')); ?>><?php _e('Nothing (disabled)', 'narato'); ?></option>
          <option value="pages" <?php selected('pages', get_narato_option('navigation')); ?>><?php _e('Pages', 'narato'); ?></option>
          <option value="categories" <?php selected('categories', get_narato_option('navigation')); ?>><?php _e('Categories', 'narato'); ?></option>
          <option value="links" <?php selected('links', get_narato_option('navigation')); ?>><?php _e('Links', 'narato'); ?></option>
          <?php if(WP_VERSION > 2.9): ?><option value="menus" <?php selected('menus', get_narato_option('navigation')); ?>><?php _e('Custom Menus', 'narato'); ?></option><?php endif; ?>
         </select>
         <div class="hidden alignleft inline opt_links">
          <?php
           $taxonomy = 'link_category';
           $args ='';
           $terms = get_terms( $taxonomy, $args );
           if ($terms): ?>
             <?php _e("from category","narato"); ?>
             <select name="navigation_links">
             <?php
             foreach($terms as $term):
              if ($term->count > 0): ?>
               <option value="<?php echo $term->name; ?>" <?php selected($term->name, get_narato_option('navigation_links')); ?>><?php echo $term->name; ?> (<?php printf(__("%s links","narato"),$term->count); ?>)</option>
              <?php endif;
             endforeach;
             ?>
             </select>
             <?php
           else: ?>
             <p class="error"><?php _e("No links found","narato"); ?></p>
           <?php
           endif;
          ?>
         </div>
        </td>
       </tr>

       <tr id="nav-list" <?php if(!get_narato_option('navigation')): ?>class="hidden"<?php endif; ?>>
        <th scope="row"><p><?php _e("Exclude from navigation","narato"); ?><span><?php _e("Check the items you wish to hide from the main menu","narato"); ?></span></p></th>
        <td>

          <?php if(get_option('show_on_front')<>'page'): ?>
          <ul class="nav-exclude">
            <li><input name="exclude_home" id="opt_exclude_home" class="checkbox" type="checkbox" value="1" <?php checked('1', get_narato_option('exclude_home')) ?> /><label> <a href="<?php echo get_settings('home'); ?>"><?php _e('Home','narato'); ?></a> </label></li>
          </ul>
          <?php endif; ?>


          <?php
           $categories = &get_categories(array('hide_empty' => false));
           $exclude_categories = explode(',', get_narato_option('exclude_categories'));
           $walker = new narato_CategoryWalker('checkbox','ul',$exclude_categories);
           if (!empty($categories)): ?>
             <ul class="hidden nav-exclude" id="category-list">
              <?php echo $walker->walk($categories, 0, array('checkboxes'=> true, 'count' => true)); ?>
             </ul>
           <?php endif; ?>

          <?php
           $pages = &get_pages('sort_column=post_parent,menu_order');
           $exclude_pages = explode(',', get_narato_option('exclude_pages'));
           $walker = new narato_PageWalker('checkbox','ul',$exclude_pages);
           if (!empty($pages)): ?>
             <ul class="hidden nav-exclude" id="page-list">
              <?php echo $walker->walk($pages, 0,array(),0); ?>
             </ul>
           <?php endif; ?>

        </td>
       </tr>
       <?php do_action("narato_admin_navigation"); ?>

      </table>
     </div>

     <div class="section" id="tab-seo">
      <table class="form-table">
       <tr>
        <th scope="row"><p><?php _e("Additional site optimization for search engines","narato"); ?><span><?php _e("Uncheck if you are using a SEO plugin!","narato"); ?></span></p></th>
        <td><input name="seo" type="checkbox" class="checkbox" id="opt_seo" value="1" <?php checked('1', get_narato_option('seo')) ?> /></td>
       </tr>

       <tr>
        <th scope="row"></th>
        <td>
         <h3><?php _e("What does this do?","narato"); ?></h3>
         <ul style="list-style: disc">
          <li><em><?php printf(__('enables canonical URLs for comments (duplicate content fix, only needed on wp < 2.9)','narato')); ?></em></li>
          <li><em><?php printf(__('generates unique titles for posts with multiple comment pages (prevents duplicate titles)','narato')); ?></em></li>
          <li><em><?php printf(__('generates a unique meta description tag for each page (no meta keywords; why? -<a %s>useless</a>)','narato'),'href="http://googlewebmastercentral.blogspot.com/2009/09/google-does-not-use-keywords-meta-tag.html" target="_blank"'); ?> </em></li>
         </ul>
        </td>
       </tr>

       <?php do_action("narato_admin_seo"); ?>

      </table>
     </div>

     <div class="section" id="tab-ads">
      <table class="form-table">

       <tr>
        <th scope="row">
         <p><?php printf(__("Advertisment blocks","narato"),$i); ?><span><?php printf(__('Use the %s short code to insert these ads into posts, text widgets or footer','narato'),'<code>[ad]</code>'); ?><br /><br><?php if(!current_user_can('unfiltered_html')) _e("Only some HTML tags and attributes are allowed",'narato'); ?></span></p><br />
         <p><span><?php _e("Example:","narato"); ?></span></p>
         <p><code>[ad code=4 align=center]</code></p>
        </th>
        <td class="clearfix">
         <?php for ($i=1; $i<=6; $i++): ?>
         <div class="ad-code clearfix">
          <label for="opt_ad_code_<?php echo $i; ?>"><?php printf(__("Ad code #%s:","narato"),$i); ?></label><br />
          <textarea rows="8" cols="40" name="ad_code_<?php echo $i; ?>" id="opt_ad_code_<?php echo $i; ?>" class="code"><?php echo wp_specialchars(get_narato_option('ad_code_'.$i)); ?></textarea>

         </div>
         <?php endfor; ?>
        </td>
       </tr>

       <?php do_action("narato_admin_ads"); ?>

      </table>
     </div>

     <div class="section" id="tab-advanced">
      <table class="form-table">
       <tr>
        <th scope="row"><p><?php _e("Use jQuery","narato"); ?><span><?php _e("For testing purposes only. Only uncheck if you know what you're doing!","narato"); ?></span></p></th>
        <td><input id="opt_jquery" name="jquery" type="checkbox" class="checkbox" value="1" <?php checked('1', get_narato_option('jquery') ) ?> /></td>
       </tr>

       <tr>
        <th scope="row"><p><?php _e("Enable AJAX for comments","narato"); ?><span><?php _e("Navigate trough comment pages and post comments without refreshing (faster load, but may be incompatible with some plugins)","narato"); ?></span></p></th>
        <td><input id="opt_ajax_comments" name="ajax_comments" type="checkbox" class="checkbox" value="1" <?php checked('1', get_narato_option('ajax_comments') ) ?> /></td>
       </tr>

       <tr>
        <th scope="row"><p><?php _e("Enable theme built-in lightbox on all image links","narato"); ?><span><?php _e("Uncheck if you prefer a lightbox plugin","narato"); ?></span></p></th>
        <td><input id="opt_lightbox" name="lightbox" type="checkbox" class="checkbox" value="1" <?php checked('1', get_narato_option('lightbox') ) ?> /></td>
       </tr>

       <tr>
        <th scope="row"><p><?php _e("Remove narato settings from the database after theme switch","narato"); ?><span><?php _e("Only check if you're planning to remove and change the theme","narato"); ?></span></p></th>      <td>
         <input name="remove_settings" id="opt_remove_settings" type="checkbox" class="checkbox" value="1" <?php checked('1', get_narato_option('remove_settings') ) ?> />
        </td>
       </tr>

       <?php do_action("narato_admin_advanced"); ?>

      </table>

      <?php if(current_user_can('edit_themes')): // disable this option for users that can't edit themes (usually on wpmu) ?>
      <hr />
      <table class="form-table">

       <tr>
        <th scope="row">
         <p><?php _e("User functions","narato"); ?><span><?php _e("PHP code to add to the theme functions. Useful if you have plugins that require you to change the theme's functions.php file","narato"); ?></span></p>
       </th>
        <td>
         <textarea rows="16" cols="60" name="functions" id="opt_functions" class="code"><?php echo wp_specialchars(get_narato_option('functions')); ?></textarea>
        </td>
       </tr>

      </table>
      <?php endif; ?>
     </div>

     <div class="section" id="tab-css">
      <table class="form-table">

       <tr>
        <th scope="row"><p><?php _e("CSS to add, or modify theme styles","narato"); ?><span><?php printf(__("Check %s to see existing theme classes and properties","narato"),'<a href="'.get_bloginfo('stylesheet_url').'">style.css</a>'); ?></span></p>
        </th>
        <td valign="top">
         <textarea rows="30" cols="80" name="user_css" id="opt_user_css" class="code"><?php echo wp_specialchars(get_narato_option('user_css')); ?></textarea>
        </td>
       </tr>

       <?php do_action("narato_admin_user_css"); ?>

      </table>
     </div>


    </div>
    <!-- /sections -->

   </div>
   <!-- /tabbed content -->

   </form>

   <div class="support">
    <form class="alignleft" action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input name="cmd" type="hidden" value="_s-xclick" /> <input name="hosted_button_id" type="hidden" value="4605915" /> <input alt="Donate" name="submit" src="<?php echo THEME_URL; ?>/admin/images/pp.gif" type="image" /></form>
    <a href="http://vivasky.com/projects/narato"><strong>narato</strong></a> is a free theme designed by <a href="http://vivasky.com">vivasky</a>.<br />You can support the development of more free resources by donating.
   </div>

  </div>
  <?php
}

function narato_add_menu() {
  $page = add_theme_page(
       __('narato settings', 'narato'),
       __('narato settings', 'narato'),
       'edit_themes',
       'theme-settings',
       'narato_theme_settings'
  );
  add_action("admin_print_scripts-$page", 'narato_setup_admin_js');
  add_action("admin_footer-$page", 'narato_admin_init_js');
  add_thickbox();
}


function narato_shortcode_list($text, $screen){
  global $shortcode_tags;
  $text .= '<br /><h5>'.__("Active shortcodes you can use in your posts or widgets","narato").'</h5><pre>';
  $text .= '<ul>';
  foreach($shortcode_tags as $shortcode => $function) $text .= '<li style="float:left; width: 160px;">['.$shortcode.']</li>';
  $text .= '</ul></pre><br clear="both" />';
  return $text;
}
add_action('contextual_help', 'narato_shortcode_list', 11, 2);

add_action("admin_print_styles", 'narato_setup_admin_css');

add_action('wp_ajax_site_preview', 'narato_get_site_preview');

add_action('admin_menu', 'narato_add_menu');
add_action('admin_post_narato_update', 'narato_update_options');
if(get_narato_option('remove_settings')) add_action('switch_theme', 'narato_remove_options');


function narato_save_meta_data($post_id){
  global $post;
  $data = stripslashes($_POST['asides']);
  if ($data && current_user_can('edit_post', $post_id)):

    if (!get_post_meta($post_id, 'asides', true)):
     add_post_meta($post_id, 'asides', '1', true);
    endif;

  else:
   delete_post_meta($post_id, 'asides');
  endif;
}


function narato_asides(){
  global $post;
 ?>
  <div class="misc-pub-section misc-pub-section-last">
    <label for="asides">
      <input name="asides" id="asides" type="checkbox" value="1" <?php checked('1', get_post_meta($post->ID, 'asides', true)) ?> /> <?php _e("Display as Asides", "narato"); ?>
    </label>
  </div>
  <?php
}
add_action('post_submitbox_misc_actions', 'narato_asides');
add_action('save_post', 'narato_save_meta_data');

?>